  <template>
  <div class="box-container" v-loading="loading">
    <el-row class="loginRow" type="flex" justify="center" align="middle">
      <el-col :sm="6" :md="8" :lg="12"> </el-col>
      <el-col :xs="18" :sm="10" :md="8" :lg="7">
        <el-form ref="form" class="loginForm" :model="form" :rules="rules">
          <p class="proName">大唐互联-边缘平台</p>
          <el-form-item label="用户名" prop="userName">
            <el-input v-model="form.userName" autocomplete="on"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input
              type="password"
              v-model="form.password"
              autocomplete="on"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-row>
              <el-col :span="12">
                <el-button type="primary" @click="login('form')"
                  >登录</el-button
                >
              </el-col>
              <el-col :span="12">
                <el-button native-type="reset" @click="resetLogin('form')"
                  >重置</el-button
                >
              </el-col>
            </el-row>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loading: false,
      form: {
        userName: "admin",
        password: "123qwe",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 4,
            max: 20,
            message: "用户名长度在4到20个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 4,
            max: 20,
            message: "密码长度在4到20个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    login(formName) {
      let that = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.loading = true;
          that.$api.system
            .login({
              UserNameOrEmailAddress: this.form.userName,
              Password: this.form.password,
              RememberClient: true,
            })
            .then((response) => {
              that.loading = false;
              if (response) {
                that.$store.commit("setToken", response.result.accessToken);
                that.$router.push("/");
              }
            })
            .catch((error) => {
              that.loading = false;
              console.log(error);
            });
        } else {
          that.loading = false;
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetLogin(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped>
.box-container {
  width: 100%;
  height: 100%;
  background: radial-gradient(ellipse at 90% 50%, white, #4481eb);
}
.loginRow {
  width: calc(100vw);
  height: calc(100vh);
  transition: height 0.8s;
  background-image: url("../../assets/login.svg");
  background-size: 55%;
  background-repeat: no-repeat;
  background-position: 5% 40%;
}

.proName {
  text-align: center;
  font-size: 24px;
  color: #333333;
  margin: 20px 0;
}

.loginForm {
  padding: 10px 20px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0px 5px 10px #cccc;
}
</style>